package acwing.算法题;/**
 * @author： li
 * @date： 2022-04-05 14:07
 * @version 1.0
 */
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
public class 亲戚_并查集 {
    static int[] fa = new int[(int) (1e6+5)];
    static int[] rank = new int[(int)(1e6+5)];
    // 查找过程, 查找元素p所对应的集合编号
    // O(h)复杂度, h为树的高度
    static int find(int x){
        // 不断去查询自己的父亲节点, 直到到达根节点
        // 根节点的特点: parent[p] == p
        while( x != fa[x] )
            x = fa[x];
        return x;
    }
    //减少树的层数来优化
    //小弟跟着大哥走 小弟的rank+1 树的层数加一
    static void join(int x,int y){
        int fx=find(x),fy=find(y);
        if(fx==fy) return;
        if(rank[fx]>rank[fy])
                fa[fy]=fx;
        else if(rank[fx]<rank[fy])
                fa[fy]=fx;
        else {
            fa[fx]=fy;
            rank[fy]++;
        }
    }
    //路径压缩
    static int find1(int x){
        if(fa[x]==x)
            return x;
        return fa[x]=find1(fa[x]);
    }
    //路径压缩后 层数都是最少的
    static void join1(int x,int y){
        int fx =find1(x);int fy=find1(y);
        if(fx!=fy)
            fa[fx]=fy;
    }
    static boolean isCon(int x,int y){
        return find1(x)==find1(y);
    }
    public static void main(String[] args) throws IOException {
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        Scanner sc  = new Scanner(System.in);
        int n=sc.nextInt();//人数
        int m =sc.nextInt();//关系数据
        for(int i=1;i<=n;i++){
            fa[i]=i;
        }
        while(m>0){
            int x=sc.nextInt();
            int y=sc.nextInt();
            join1(x,y);
            m--;
        }
        int q =sc.nextInt();
        while(q>0){
            int j =sc.nextInt();
            int k =sc.nextInt();
            if(isCon(j,k))
                out.write("YES\n");
            else
                out.write("NO\n");
            q--;
        }
        out.flush();
    }
}
